<!--包含头部文件-->
{include file="public/header" /}
<div class="page-container">
    <div>
       <form action="" method="post" id="form-purchase-add">
           <table class="table ">
               <thead>
               <tr>
                   <th width="60px">商品名</th>
                   <td>
                       {$title}
                   </td>
                   <input type="hidden" class="input-text value" name="title" value="{$title}" readonly>
                   <input type="hidden" class="input-text value" name="tb_id" value="{$id}" readonly>
                   <input type="hidden" class="input-text value" name="is_tmall" value="{$is_tmall}" readonly>
                   <input type="hidden" class="input-text value" name="cat_id" value="{$cate_id}" readonly>
                   <input type="hidden" class="input-text value" name="activityid" value="{$activityid}" readonly>
                   <input type="hidden" class="input-text value" name="couponmoney" value="{$couponmoney}" readonly>
               </tr>
               </thead>
               <tbody>
               <tr>
                   <th>分类</th>
                   <td> {$cate_name|default=''}</td>
               </tr>
               <tr>
                   <th>链接</th>
                   <td><a href="{$url}" target="_blank" title="{$title}" style="color: deepskyblue">{$url}</a></td>
                   <input type="hidden" class="input-text value" name="url" value="{$url}">
               </tr>
               <tr>
                   <th>采购需求</th>
                   <td>
                       <table class="table table-border table-bordered">
                           <thead>
                           <tr>
                               <th>淘宝规格</th>
                               <th>价格</th>
                               <th>采购数</th>
                           </tr>
                           </thead>
                           <tbody>
                           {volist name='list' id='vo'}
                           <tr class="caigou">
                               <td>
                                   {$vo.names}
                                   <input type="hidden" class="input-text value" name="sku_name[]" value="{$vo.names}">
                               </td>
                               <td><input type="text" class="input-text value" style="width: 50%" name="price[]" value="{$vo.price}" readonly onchange="add_up1(this)"></td>
                               <td><input type="text" class="input-text value" style="width: 50%" name="num[]" value="" placeholder="请输入采购数" onchange="add_up2(this)"></td>
                           </tr>
                           {/volist}
                           </tbody>
                       </table>
                   </td>
               </tr>
               <tr>
                   <th>总价</th>
                   <td style="color: red">
                       <input type="text" class="input-text value" id="total" style="width: auto" name="total" value="0.00">
                   </td>
               </tr>
               <tr>
                   <th>收货店铺</th>
                   <td>
                       <span class="select-box inline">
                       <select name="shop" class="select">
                           <option value="0">请选择店铺</option>
                           {volist name="shops" id="vo"}
                           <option value="{$vo.id}">{$vo.shop_name}</option>
                           {/volist}
                       </select>
                       </span>
                   </td>
               </tr>
               <tr>
                   <th></th>
                   <td>
                       <span class="select-box inline">
                       <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                       </span>
                   </td>
               </tr>
               </tbody>
           </table>
       </form>
    </div>
</div>
<!--包含footer文件-->
{include file="public/footer" /}

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="__STATIC__/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="__STATIC__/lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="__STATIC__/lib/laypage/1.2/laypage.js"></script>
<script type="text/javascript" src="__STATIC__/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="__STATIC__/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="__STATIC__/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript">
    function add_up1(a) {
        var total=0;
        var price;
        var num;
        var money;
        $(".caigou").each(function (i) {
            price = $('.caigou').eq(i).find('input').eq(1).val()
            num = $('.caigou').eq(i).find('input').eq(2).val()
            if (price == ''){
                price =0;
            }
            if (num == ''){
                num =0;
            }
            money = price * num;
            total = total + money;
            price =0;
            num=0;
        })
        $("#total").val(total)
    }
    function add_up2(a) {
        var total=0;
        var price;
        var num;
        var money;
        $(".caigou").each(function (i) {
            price = $('.caigou').eq(i).find('input').eq(1).val()
            num = $('.caigou').eq(i).find('input').eq(2).val()
            if (price == ''){
                price =0;
            }
            if (num == ''){
                num =0;
            }
            money = price * num;
            total = total + money;
            price =0;
            num=0;
        })
        $("#total").val(total.toFixed(2))
    }
$(function () {

    $('.skin-minimal input').iCheck({
        checkboxClass: 'icheckbox-blue',
        radioClass: 'iradio-blue',
        increaseArea: '20%'
    });

    $("#form-purchase-add").validate({
        rules:{
            title:{
                required:true,
            },
            tb_id:{
                required:true,
            },
            is_tmall:{
                required:true,
            },
            cat_id:{
                required:true,
            },
            activityid:{
                required:true,
            },
            couponmoney:{
                required:true,
            },
            url:{
                required:true,
            },
            total:{
                required:true,
            },
            shop:{
                required:true,
            },
        },
        onkeyup:false,
        focusCleanup:true,
        success:"valid",
        submitHandler:function(form){
            var arr = new FormData(document.getElementById("form-purchase-add"));
            $.ajax({
                url:"{:url('goods/purchase_save')}",
                type:"post",
                data:arr,
                async: true,
                processData: false,
                contentType: false,
                dataType:"json",
                success:function (data) {
                    if (data.code == 200){
                        layer.alert('添加成功',{
                            skin: 'layui-layer-molv' //样式类名 自定义样式
                            ,closeBtn: 0  // 是否显示关闭按钮
                            ,anim: 1 //动画类型
                            ,btn: '确认'//按钮
                            ,icon: 6  // icon
                            ,yes:function(){
                                layer.closeAll();
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        })
                    }else {
                        layer.alert('添加失败'+data.msg+'',{
                            skin: 'layui-layer-molv' //样式类名 自定义样式
                            ,closeBtn: 0  // 是否显示关闭按钮
                            ,anim: 1 //动画类型
                            ,btn: '确认'//按钮
                            ,icon: 6  // icon
                        })
                    }

                },
                error:function () {
                    layer.alert('添加失败',{
                        skin: 'layui-layer-molv' //样式类名 自定义样式
                        ,closeBtn: 0  // 是否显示关闭按钮
                        ,anim: 1 //动画类型
                        ,btn: '确认'//按钮
                        ,icon: 6  // icon
                    })
                }
            })

        }
    });
    
})

</script>
</body>
</html>